package com.aaa.tars.controller;

import com.aaa.tars.entity.ModifyPwd;
import com.aaa.tars.entity.Role;
import com.aaa.tars.entity.User;
import com.aaa.tars.service.RoleService;
import com.aaa.tars.service.UserService;
import com.aaa.tars.util.OssUtil;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.Result;
import com.aaa.tars.util.ResultUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(User)表控制层
 *
 * @author makejava
 * @since 2025-07-09 16:46:25
 */
@RestController
@RequestMapping("user")
@Log4j2
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    /**
     * 登录
     * @param userName 用户名
     * @param password 密码
     * @return 登录结果
     */
    @GetMapping("login")
    public Result login(String userName,String password) {
        return userService.checkUser(userName,password);
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("logout")
    public Result logout(HttpSession session) {
        return userService.logout(session);
    }


    /**
     * 修改密码
     * @param passwordDto
     * @param session
     * @return
     */
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestBody ModifyPwd passwordDto, HttpSession session) {
        User currentUser = (User) session.getAttribute("userInfo");
        if(currentUser == null) {
            return ResultUtil.error("请先登录");
        }
        //打印输入修改密码
        System.out.println("输入修改密码控制："+passwordDto);
        return userService.updatePassword(passwordDto, currentUser.getUserId());
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<User> page) {
        return ResultUtil.success(this.userService.queryByPage(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public Result queryById(Integer userId) {
        return ResultUtil.success(this.userService.queryById(userId));
    }

    /**
     * 获取当前登录用户信息
     *
     * @param session HttpSession对象
     * @return 当前登录用户信息
     */
    @GetMapping("getCurrentUserInfo")
    public Result getCurrentUserInfo(HttpSession session) {
        User currentUser = (User) session.getAttribute("userInfo");
        if (currentUser == null) {
            return ResultUtil.error("用户未登录");
        }
        return ResultUtil.success(currentUser);
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody User user) {
        return ResultUtil.success(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody User user) {
        return ResultUtil.success(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param userId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer userId) {
        return ResultUtil.success(this.userService.deleteById(userId));
    }

    //获取用户角色
    @GetMapping("/roles/{userId}")
    public Result<List<Role>> getUserRoles(@PathVariable Integer userId) {
        List<Role> roles = userService.getRolesByUserId(userId);
        return ResultUtil.success(roles);
    }

    @PostMapping("/assignRoles")
    public Result<String> assignRoles(@RequestBody Map<String, Object> params) {
        Integer userId = (Integer) params.get("userId");
        List<Integer> roleIds = (List<Integer>) params.get("roleIds");

        userService.assignRoles(userId, roleIds);
        return ResultUtil.success("角色分配成功");
    }

    //获取所有角色
    @GetMapping("/roles/all")
    public Result<List<Role>> getAllRoles() {
        List<Role> roles = roleService.queryAll();
        return ResultUtil.success(roles);
    }

    //上传图片
    @PostMapping("uploadPic")
    public Result uploadPic(@RequestParam("pic") MultipartFile file){
        //调用工具类，进行上传
        return ResultUtil.success(OssUtil.uploadFile("user/images",file));
    }
}

